/**
 * 易优CMS
 * ============================================================================
 * 版权所有 2016-2028 海南赞赞网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.eyoucms.com
 * ----------------------------------------------------------------------------
 * 如果商业用途务必到官方购买正版授权, 以免引起不必要的法律纠纷.
 * ============================================================================
 * Author: 小虎哥 <1105415366@qq.com>
 * Date: 2020-1-1
 */

const App = getApp();
const func = require('../../utils/func.js');
import wxParse from '../../wxParse/wxParse.js'; // 富文本插件

Page({

  data: {
    swiperImageList:[
      {litpic:'https://28323801.s61i.faiusr.com/2/AD0I2d-ADRACGAAgpNqgjQYozM7f5gUw7gU4gwM.jpg'},
      {litpic:'https://28323801.s61i.faiusr.com/2/AD0I2d-ADRACGAAgpNqgjQYo7e7Sogcw7gU4gwM.jpg'}
    ],
    navList:[
      {img:'https://28323801.s61i.faiusr.com/4/AD0I2d-ADRAEGAAg_ZedjQYoxvK86gIwZDhk.png', label:'限时优惠'},

      // {img:'https://28692981.s61i.faiusr.com/4/AD0I9aPXDRAEGAAg6daYkAYojtj90wEwcDhw.png', label:'生活服务'},
      {img:'https://28323801.s61i.faiusr.com/4/AD0I2d-ADRAEGAAg_ZedjQYo4MG0zAIwZDhk.png"', label:'电子数码'},
      {img:'https://28323801.s61i.faiusr.com/4/AD0I2d-ADRAEGAAg1ZidjQYo-MLW0QQwZDhk.png', label:'热销榜单'},
      // {img:'https://28692981.s61i.faiusr.com/4/AD0I9aPXDRAEGAAg_9aYkAYo5NHgvAUwcDhw.png', label:'送货拉货'},
      {img:'https://28323801.s61i.faiusr.com/4/AD0I2d-ADRAEGAAg_ZedjQYo8IG1oQMwZDhk.png', label:'海外优选'},
      // {img:'https://28421286.s61i.faiusr.com/4/AD0IptnGDRAEGAAggdrqjQYo386a6Acwbzhv.png"', label:'海鲜水产'},
      {img:'https://28692981.s61i.faiusr.com/4/AD0I9aPXDRAEGAAg29WYkAYomLWx_gYwcDhw.png', label:'餐饮美食'},
    
    ],
    seckillList:[
      {img:'https://28692981.s61i.faiusr.com/2/AD0I9aPXDRACGAAgseimkAYorcyvzAMw7gU47gU!400x400.jpg', name:'秋叶檀香大牌商品', Price:'399', oldPrice:'599'},
      {img:'https://28323801.s61i.faiusr.com/2/AD0I2d-ADRACGAAgl4CdjQYoo9ewywEw7gU47gU!400x400.jpg', name:'法式亮片女细跟高跟鞋', Price:'599', oldPrice:'799'},
      {img:'https://28421286.s61i.faiusr.com/2/AD0IptnGDRACGAAgw6TrjQYor_CELjDuBTjnBA.jpg', name:'波士顿大龙虾', Price:'399', oldPrice:'599'},
      {img:'https://28421286.s61i.faiusr.com/4/AD0IptnGDRAEGAAgjaXrjQYojt_F8AUwmwE4mAE.png', name:'蒜蓉麻辣小龙虾', Price:'399', oldPrice:'599'},
    ]
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
    let _this = this;
    _this.getPageData(); // 加载页面数据
  },
  /**
   * 生命周期回调—监听页面显示
   */
  onShow() {

  },
  /**
   * 加载页面数据
   */
  getPageData: function () {
    let _this = this;
    App._requestApi(_this, App.globalData.config.apiIndexUrl, {
      apiAdv_1: `ekey=1&pid=3`, // 广告位标签adv
      apiArclist_1: `ekey=1&channel=2&limit=8&orderby=rand`, // 第一个文档列表标签arclist
      apiArclist_2: `ekey=2&typeid=12&limit=3`, // 第二个文档列表标签arclist
      apiArclist_3: `ekey=3&typeid=4&limit=4`, // 第三个文档列表标签arclist
      apiType_1: `ekey=1&typeid=8&addfields=content`, // 指定栏目标签type
      apiDiscount_1: `ekey=1&activeid=&limit=`, // 限时折扣标签discount,不传activeid默认选用当前正在进行的或者即将开始的
      // apiDiscount_1: `ekey=1&activeid=1`, // 限时折扣标签discount,不传activeid默认选用当前正在进行的或者即将开始的
      apiGlobal: `ekey=1`, // 全局配置变量标签global
      // 这里可以根据需求填写更多的api标签
    }, function (res) {
      // 特别说明：中括号[1]的数字必须与api标签的参数ekey=1值对应，否则数据对不上。
      let adv_1 = res.data.apiAdv[1], // adv广告位数据
        arclist_1 = res.data.apiArclist[1], // 第一个arclist文档列表数据
        arclist_2 = res.data.apiArclist[2], // 第二个arclist文档列表数据
        arclist_3 = res.data.apiArclist[3], // 第三个arclist文档列表数据
        type_1 = res.data.apiType[1], // type指定栏目数据
        disCount_1 =  res.data.apiDiscount[1], 
        global = res.data.apiGlobal[1]; // global全局配置变量数据
        let DiscountList = disCount_1.goodsList,
        Discountactive = disCount_1.active;
        let started = '';
        if(DiscountList){
          if (Discountactive.active_status == 10){
            started = Discountactive.end_date - Date.parse(new Date())  / 1000     //已经开始时间差
          } else{
            started =Discountactive.start_date  - Date.parse(new Date() ) / 1000    //即将开始时间差
          }
        }
      // html富文本转码
      if (undefined != type_1.data.content) {
        wxParse.wxParse('type_1_content', 'html', type_1.data.content, _this, 0);
      }
      // 设置导航标题
      wx.setNavigationBarTitle({
        title: global.data.web_name || '易优CMS小程序'
      })
      // var totalSecond = ( 2 * 60 * 60 * 1000 - cha) / 1000;
   
      _this.setData({
        adv_1,
        arclist_1,
        arclist_2,
        arclist_3,
        type_1,
        disCount_1,
        DiscountList,
        global,
        Discountactive,
        started
      })
      var interval = setInterval(function () {
        var second = started ;

        var day = Math.floor(second / 3600 / 24);
        var hr = Math.floor((second - day * 3600 * 24) / 3600);
        var hrStr = hr.toString();
        if (hrStr.length == 1) hrStr = '0' + hrStr;
        // 分钟位
        var min = Math.floor((second - day * 3600 * 24 - hr * 3600) / 60);
        var minStr = min.toString();
        if (minStr.length == 1) minStr = '0' + minStr;

        // 秒位
        var sec = second - day * 3600 * 24 - hr * 3600 - min * 60;
        var secStr = sec.toString();
        if (secStr.length == 1) secStr = '0' + secStr;

        _this.setData({
          countDownDay: day,
          countDownHour: hrStr,
          countDownMinute: minStr,
          countDownSecond: secStr,
        });
        started--;
      }.bind(this), 1000);
    });
  },
  /**
   * 下拉刷新
   */
  onPullDownRefresh: function () {
    this.getPageData(); // 获取首页数据
    wx.stopPullDownRefresh(); // 停止下拉刷新
  },
  /**
   * 分享当前页面
   */
  onShareAppMessage() {
    let _this = this;
    return {
      title: _this.data.page.params.title,
      path: "/pages/index/index?" + App.getShareUrlParams()
    };
  },
  /**
   * 分享到朋友圈
   */
  onShareTimeline() {
    let _this = this;
    return {
      title: _this.data.page.params.title,
    }
  },
  /**
   * 跳转列表页
   */
  jumpList(e) {
    func.jumpList(e)
  },
  /**
   * 跳转详情页
   */
  jumpView(e) {
    func.jumpView(e)
  },
   /**
   * 跳转限时折扣详情页
   */
  jumpdiscount(e) {
      let field = e.currentTarget.dataset.field;
      console.log(e)
			if (!field) {
				this.showError('jumpView事件的同一元素上缺少属性 data-field')
				return false;
			}
			let aid = field.aid,
      active_id = field.active_id;
      let url = `/pages/archives/product/discount_detail?aid=${aid}&active_id=${active_id}`
      wx.navigateTo({
        url: url
      })
  },
  jumpdiscountList(e){
    let active_id = e.currentTarget.dataset.field
    let url = `/pages/discount/index?active_id=${active_id}`
    wx.navigateTo({
      url: url,
    })
  },
  /**
   * 点击拨打电话
   */
  makePhoneCall: function (e) {
    let mobile = e.currentTarget.dataset.mobile
    wx.makePhoneCall({
      phoneNumber: mobile
    })
  },
  /**
   * 地图导航
   */
  gomap: function (e) {
    let coordinate = e.currentTarget.dataset.coordinate || '19.992555,110.339932';
    if (coordinate) {
      let address = e.currentTarget.dataset.address || '';
      let map = coordinate.split(',');
      wx.openLocation({
        latitude: parseFloat(map[0]),
        longitude: parseFloat(map[1]),
        address: address
      })
    }
  },
});